package com.zhike.file.imfile.repository;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhike.file.imfile.mapper.FileMetaDataInfoMapper;
import com.zhike.file.imfile.model.FileMetaDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.liuxp.minioplus.api.model.dto.FileMetadataInfoDTO;
import org.liuxp.minioplus.api.model.dto.FileMetadataInfoSaveDTO;
import org.liuxp.minioplus.api.model.dto.FileMetadataInfoUpdateDTO;
import org.liuxp.minioplus.api.model.vo.FileMetadataInfoVo;
import org.liuxp.minioplus.core.repository.MetadataRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文件元数据接口实现类
 *
 * @author contact@liuxp.me
 * @since 2024/05/22
 */
@Slf4j
@Service
public class MetadataRepositoryImpl implements MetadataRepository {
    @Autowired
    FileMetaDataInfoMapper baseMapper;

    @Override
    public List<FileMetadataInfoVo> list(FileMetadataInfoDTO searchDTO) {

        // 组装查询参数
        QueryWrapper<FileMetaDataInfo> queryWrapper = buildParams(searchDTO);

        List<FileMetaDataInfo> FileMetaDataInfoList = baseMapper.selectList(queryWrapper);

        List<FileMetadataInfoVo> fileMetadataInfoVoList = new ArrayList<>();

        for (FileMetaDataInfo FileMetaDataInfo : FileMetaDataInfoList) {
            FileMetadataInfoVo fileMetadataInfoVo = new FileMetadataInfoVo();
            BeanUtils.copyProperties(FileMetaDataInfo, fileMetadataInfoVo);
            fileMetadataInfoVo.setStorageBucket(FileMetaDataInfo.getStorageBucket());
            fileMetadataInfoVo.setStoragePath(FileMetaDataInfo.getStoragePath());
            fileMetadataInfoVoList.add(fileMetadataInfoVo);
        }

        return fileMetadataInfoVoList;
    }

    @Override
    public FileMetadataInfoVo one(FileMetadataInfoDTO searchDTO) {

        // 组装查询参数
        QueryWrapper<FileMetaDataInfo> queryWrapper = buildParams(searchDTO);
        queryWrapper.last("limit 1");

        FileMetaDataInfo FileMetaDataInfo = baseMapper.selectOne(queryWrapper);

        FileMetadataInfoVo fileMetadataInfoVo = new FileMetadataInfoVo();

        if (null != FileMetaDataInfo) {
            BeanUtils.copyProperties(FileMetaDataInfo, fileMetadataInfoVo);
            fileMetadataInfoVo.setStorageBucket(FileMetaDataInfo.getStorageBucket());
            fileMetadataInfoVo.setStoragePath(FileMetaDataInfo.getStoragePath());
        }

        return fileMetadataInfoVo;
    }

    @Override
    public FileMetadataInfoVo save(FileMetadataInfoSaveDTO saveDTO) {

        FileMetaDataInfo FileMetaDataInfo = new FileMetaDataInfo();
        BeanUtils.copyProperties(saveDTO, FileMetaDataInfo);
        FileMetaDataInfo.setCreateTime(new Date());
        FileMetaDataInfo.setUpdateTime(new Date());
        FileMetaDataInfo.setStorageBucket(saveDTO.getStorageBucket());
        FileMetaDataInfo.setStoragePath(saveDTO.getStoragePath());

        int result = baseMapper.insert(FileMetaDataInfo);

        FileMetadataInfoVo fileMetadataInfoVo = new FileMetadataInfoVo();
        if (result == 1) {
            BeanUtils.copyProperties(FileMetaDataInfo, fileMetadataInfoVo);
        }

        return fileMetadataInfoVo;
    }

    @Override
    public FileMetadataInfoVo update(FileMetadataInfoUpdateDTO updateDTO) {

        FileMetaDataInfo FileMetaDataInfo = new FileMetaDataInfo();
        BeanUtils.copyProperties(updateDTO, FileMetaDataInfo);
        FileMetaDataInfo.setUpdateTime(new Date());
        int result = baseMapper.updateById(FileMetaDataInfo);

        FileMetadataInfoVo fileMetadataInfoVo = new FileMetadataInfoVo();
        if (result == 1) {
            BeanUtils.copyProperties(FileMetaDataInfo, fileMetadataInfoVo);
        }

        return fileMetadataInfoVo;
    }

    @Override
    public Boolean remove(Long id) {
        if (baseMapper.deleteById(id) == 1) {
            return true;
        }
        return false;
    }

    private QueryWrapper<FileMetaDataInfo> buildParams(FileMetadataInfoDTO searchDTO) {
        // 组装查询参数
        QueryWrapper<FileMetaDataInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getFileKey()), "file_key", searchDTO.getFileKey());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getFileMd5()), "file_md5", searchDTO.getFileMd5());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getBucket()), "bucket", searchDTO.getBucket());
        queryWrapper.eq(null != searchDTO.getIsPrivate(), "is_private", searchDTO.getIsPrivate());
        queryWrapper.eq(CharSequenceUtil.isNotBlank(searchDTO.getCreateUser()), "create_user",
                searchDTO.getCreateUser());

        return queryWrapper;
    }
}
